<?php
namespace app\face\controller;
use think\Controller; 
use think\Db;
use think\Log;
use app\service\wotuService;
class Appuser extends Controller
{
    public function door()
    {
        //session("url",request()->url());
        return $this->fetch();
    }
	public function test()
	{
		$guid="200F006B66304745B7DD6B58756F8671";
		$res=(new wotuService())->tupian($guid);
		echo "<pre>";
		$num=count($res["data"]);
		echo $num;echo "<br/>";
		var_dump($res["data"]);
	}
	public function index()
	{
		$user_id=request()->get("user_id");
		$userinfo=Db::table("face_user_room")->where("user_id",$user_id)->find();
		$this->assign("userinfo",$userinfo);
		return $this->fetch();
	}
	public function piclist()
	{
		$user_id=request()->get("user_id");
		$userinfo=Db::table("face_user_room")->where("user_id",$user_id)->find();
		
		if($userinfo["guid"]=="0")
		{
			$type="11";			
			$re=(new wotuService())->reg($userinfo["nickname"],$userinfo["phone"],$type,$userinfo["room_address"],"");
			$result=json_decode($re,true);
			$guid=$result["data"]["guid"];
			Db::table("face_user_room")->where("user_id",$user_id)->update(["guid"=>$guid]);
			$userinfo["guid"]=$guid;
		}
		
		$this->assign("userinfo",$userinfo);
		$res=(new wotuService())->tupian($userinfo["guid"]);
		$num=count($res["data"]);
		$this->assign("data",$res["data"]);
        $this->assign("num",$num);
		return $this->fetch();
	}
	public function upload()
	{
        $user_id=request()->get("user_id");
        $userinfo=Db::table("face_user_room")->where("user_id",$user_id)->find();
		$this->assign("userinfo",$userinfo);
        return $this->fetch();
    }
    //上传图片处理
    public function uploadAction()
    {
    	$user_id=request()->post("user_id");
    	$url=url("piclist")."?user_id=".$user_id;
        $image=str_replace("data:image/jpeg;base64,","",request()->post('image'));
        $userinfo=Db::table("face_user_room")->where("user_id",$user_id)->find();
        $guid=$userinfo["guid"];
        $result=(new wotuService())->uploadimg($image,$guid);
        if($result["code"]=="GS_SUS600"){
           	//查询该小区下的人脸设备并授权
           	$locklist=Db::table("face_lock")->where("community_id",$userinfo["community_id"])->select();
           	$deviceKeys="";
           	foreach ($locklist as $key => $value) {
           		$deviceKeys=$deviceKeys.$value["devicekey"].",";
           	}
           	$deviceKeys=rtrim($deviceKeys,",");
            //$deviceKeys="84E0F420854401F8,84E0F42085C601F8,84E0F42088D301F8,84E0F4213A2901FA,84E0F42157B306B2,84E0F420386B02F8";
            (new wotuService())->accredit($guid, $deviceKeys);
            
            echo "<script>alert('图片上传成功');location.href='$url';</script>";
        }else{
            echo "<script>alert('图片上传失败，请重新上传');history.go(-1);</script>";
            die;
        }
    }
    public function delimage(){
        $guid=request()->post("photoid");
        $personGuid=request()->post("personGuid");
        $result=(new wotuService())->delimage($personGuid,$guid);
        //图片删除成功
        if($result["code"]=="GS_SUS601"){
            
            $arr=array("errcode"=>"0","msg"=>"图片删除成功");
            echo json_encode($arr);
        }else{
            $arr=array("errcode"=>"1","msg"=>"图片删除失败");
            echo json_encode($arr);
        }
    }
}